﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace KTSonline
{
    public partial class CartDetail : System.Web.UI.Page
    {
        public ShoppingCart cart;
        public TAI_KHOAN tk = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            Global.currentpage = Global.Pages.CartDetail;

            if (Session["cart"] == null)
                Response.Redirect("Default.aspx");
            cart = (ShoppingCart)Session["cart"];
            Repeater1.DataSource = cart.lstCart;
            Repeater1.DataBind();

            if (Session["user"] != null)
                tk = (TAI_KHOAN)Session["user"];

            if (tk != null)
            {
                DName.Text = tk.hoten;
                DPhone.Text = tk.dienthoai;
                DAddress.Text = tk.diachi;
                DEmail.Text = tk.email;
            }
        }

        public String getTotal(object a, object b)
        {
            Int32 rs = Int32.Parse(a.ToString()) * Int32.Parse(b.ToString());
            return rs.ToString("N0");
        }

        protected void btndone_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                HOA_DON hd = new HOA_DON();
                hd.diachi = DAddress.Text;
                hd.ngaygiaomongmuon = "";
                hd.ngaylaphd = DateTime.Now;
                hd.sodt = DPhone.Text;
                hd.tenkhachhang = DName.Text;
                hd.thoigianmongmuon = "";
                hd.tongtien = cart.totalprice;
                hd.trangthai = 0;
                hd.yeucau = DMessage.Text;
                hd.email = DEmail.Text;

                foreach (ProductCart pc in cart.lstCart)
                    hd.CHI_TIET_HOA_DONs.Add(new CHI_TIET_HOA_DON(pc));

                KTSonlineDataContext db = new KTSonlineDataContext();
                db.HOA_DONs.InsertOnSubmit(hd);
                db.SubmitChanges();

                Session["ordersuccess"] = true;
                Session["cart"] = null;
                Response.Redirect("OrderSuccessful.aspx");
            }
        }
    }
}
